package com.aps.server.controller;

import cn.hutool.core.collection.CollUtil;
import com.aps.algorithm.entity.BasicEquipSchedulingResult;
import com.aps.algorithm.parameter.GanttChartParameter;
import com.aps.common.entity.Result;
import com.aps.server.dto.Device;
import com.aps.server.dto.GanttChartDto;
import com.aps.server.service.ApsSchedulingResultService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jsy
 * @description: APS排产结果控制器
 * @date 2025/10/10 10:17
 */
@Slf4j
@RestController
@RequestMapping("/api/aps-scheduling-results")
@RequiredArgsConstructor
@Tag(name = "APS排产结果", description = "APS排产结果相关的API接口")
public class ApsSchedulingResultController {
    @Resource
    ApsSchedulingResultService apsSchedulingResultService;

    /**
     * @description: 查询排产方案
     * @author: jsy
     * @time: 2025/10/10 11:02
     */
    @PostMapping("/query-scheduling-results")
    @Operation(summary = "APS查询排产方案", description = "查询排产方案")
    public Result<List<BasicEquipSchedulingResult>> querySchedulingPlans(@Parameter(description = "排产ID") @RequestParam("schedulingId") String schedulingId) {
        try {
            List<BasicEquipSchedulingResult> schedulingResults = apsSchedulingResultService.querySchedulingResults(schedulingId);
            if (CollUtil.isNotEmpty(schedulingResults)) {
                return Result.success(schedulingResults, "查询排产方案成功");
            } else {
                return Result.error(400, "未查询到排产方案数据");
            }
        } catch (Exception e) {
            log.error("创建APS约束规则失败", e);
            return Result.error(500, "查询排产方案失败: " + e.getMessage());
        }
    }

    /**
     * @description: 选择排产方案
     * @author: jsy
     * @time: 2025/10/10 14:14
     */
    @PostMapping("/select-scheduling-plan")
    @Operation(summary = "APS选择排产方案", description = "选择排产方案")
    public Result<Boolean> selectSchedulingPlan(@Parameter(description = "排产方案") @RequestParam("plan") String plan
            , @Parameter(description = "排产方案序号") @RequestParam("planIndex") String planIndex) {
        try {
            apsSchedulingResultService.selectSchedulingPlan(plan, planIndex);
            return Result.success(true, "选择排产方案成功");
//             List<BasicEquipSchedulingResult> schedulingResults = basicEquipSchedulingResultMapper.selectList(
//                    new LambdaQueryWrapper<BasicEquipSchedulingResult>()
//                            .eq(BasicEquipSchedulingResult::getPlan, plan));
        } catch (Exception e) {
            log.error("选择排产方案失败", e);
            return Result.error(500, "选择排产方案失败: " + e.getMessage());
        }
    }

    /**
     * @description: 查询甘特图
     * @author: jsy
     * @time: 2025/10/10 14:14
     */
    @PostMapping("/query-gantt-chart")
    @Operation(summary = "查询甘特图", description = "查询甘特图")
    public Result<List<Device>> queryGanttChart(@RequestBody GanttChartParameter parameter) {
        try {
            List<Device> schedulingResults = apsSchedulingResultService.queryGanttChart(parameter);
            if (CollUtil.isNotEmpty(schedulingResults)) {
                return Result.success(schedulingResults, "查询甘特图成功");
            } else {
                return Result.error(400, "未查询到甘特图数据");
            }
        } catch (Exception e) {
            log.error("查询甘特图失败", e);
            return Result.error(500, "查询甘特图失败: " + e.getMessage());
        }
    }
}
